import { type FC, type PropsWithChildren, useState } from 'react';

import { globalConfig } from '@/config';
import { setLng } from '@/locales';
import { localStg } from '@/utils/storage';

import { LangContext } from './langContext';

export const LangProvider: FC<PropsWithChildren> = ({ children }) => {
  const [locale, setLocale] = useState<App.I18n.LangType>(globalConfig.defaultLang);

  const changeLocale = (lang: App.I18n.LangType) => {
    setLng(lang);
    setLocale(lang);
    localStg.set('lang', lang);
  };

  return (
    <LangContext value={{ locale, localeOptions: globalConfig.defaultLangOptions, setLocale: changeLocale }}>
      {children}
    </LangContext>
  );
};
